//
//  ModelService.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/27.
//

#import "ModelService.h"

NSString *const kModelServicePhone = KEY_phone;
NSString *const kModelServiceEmail = KEY_email;
NSString *const kModelServiceWhatsApp = KEY_whatsApp;

@interface ModelService ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelService

@synthesize phone = _phone;
@synthesize email = _email;
@synthesize whatsApp = _whatsApp;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.phone = [self objectOrNilForKey:kModelServicePhone fromDictionary:dict];
            self.email = [self objectOrNilForKey:kModelServiceEmail fromDictionary:dict];
            self.whatsApp = [self objectOrNilForKey:kModelServiceWhatsApp fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.phone forKey:kModelServicePhone];
    [mutableDict setValue:self.email forKey:kModelServiceEmail];
    [mutableDict setValue:self.whatsApp forKey:kModelServiceWhatsApp];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


@end
